<?php

namespace App\Http\Controllers\Album;

use Illuminate\Http\Request;

use DB;
use App\Album;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class albumController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        //相册列表
        $album = Album::get();
        return view('album.index',['album'=>$album]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStore(Request $request)
    {
        //
        $album = New Album;
        $album->name = $request->name;
        $album->cover = $request->cover;
        $album->intro = $request->intro;
        if($request -> hasFile('cover')){
            //创建文件的名字
            $filename = time().rand(10000,99999);
            //获取文件的后缀
            $suffix = $request->file('cover')->getClientOriginalExtension();
            //上传路径
            $dirname = './uploads/album/';
            //文件名
            $name = $filename.'.'.$suffix;
            //上传头像
            $request->file('cover')->move($dirname,$name);
            //
            $album -> cover = trim($dirname.$name,'.');
        }
        if($album->save()){
            return redirect('/album');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getShow($id)
    {
        //
        $album = DB::table('albums')->where('id','=',$id)->get()[0];
        $picture = DB::table('pictures')->where('pid','=',$id)->get();
        return view('album.show',['picture'=>$picture,'album'=>$album]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request, $id)
    {
        $album = $request->except('_token','_method');
        if($request -> hasFile('cover')){
             //获取原来的相册图片 并删除
            $data = DB::table('albums')->where('id','=',$id)->get()[0];
            $cover = $data->cover;
            //删除商品主图片
            @unlink('.'.$cover);
            //创建文件的名字
            $filename = time().rand(10000,99999);
            //获取文件的后缀
            $suffix = $request->file('cover')->getClientOriginalExtension();
            //上传路径
            $dirname = './uploads/album/';
            //文件名
            $name = $filename.'.'.$suffix;
            //上传头像
            $request->file('cover')->move($dirname,$name);
            //
            $album ['cover'] = trim($dirname.$name,'.');
        }
        $res = DB::table('albums')->where('id','=',$id)->update($album);
        if(!empty($res)){
            return redirect("/album/show/$id") ;
        }else{
            return back();
        }
    }
    /**
     * 添加相片
     */
    public function postCreate(Request $request,$id)
    {
        $haha= $request->name;
        $content = $request->content;
        if($request->hasFile('photo')){
            $data =[];
            // $data = $request->except('_token');

            foreach($request->file('photo')as $k => $v){
                $temp = [];
                $temp['name'] = $haha;
                $temp['content'] =$content;
                $temp['pid'] = $id;
                //定义文件夹位置
                $filename = time().rand(10000,99999);
                //获取文件的后缀
                $suffix = $v->getClientOriginalExtension();
                //上传路径
                $dirname = './uploads/albums/';
                //文件名
                $name = $filename.'.'.$suffix;
                //上传头像
                $v->move($dirname,$name);
                //
                $temp['path'] = trim($dirname.$name,'.');
                //压入到data中
                $data[] = $temp;

            }
            $res = DB::table('pictures')->insert($data);
            if($res){
                return redirect("/album/show/$id")->with('info','添加成功');
            }else{
                return back()->with('info','添加失败');
            }

        }
        return redirect("/album/show/$id")->with('info','插入成功');

    }

    /**
     * 修改单相片
     */
    public function postEdit(Request $request,$id)
    {
        $data = $request->except('_token','pid');
        $pid = $request->pid;
        $res = DB::table('pictures')->where('id','=',$id)->update($data);
        if($res){
            return redirect("/album/show/$pid")->with('info','插入成功');
        }else{
            return back();
        }
    }
    /**
     * 删除单相片
     */
    public function postDel(Request $request,$id)
    {
        //获取图片信息
        $data = DB::table('pictures')->where('id','=',$id)->get()[0];
        $path = $data->path;
        //删除商品主图片
        @unlink('.'.$path);
        $pid = $request->pid;
        $res = DB::table('pictures')->where('id','=',$id)->delete();
        if($res){
            return redirect("/album/show/$pid");
        }else{
            return back();
        }
    }

    /**
     * Remove the specified resource from storage.
     * @return \Illuminate\Http\Response
     */
    public function postDelete($id)
    {
        //获取图片信息
        $data = DB::table('pictures')->where('pid','=',$id)->get();
        
        foreach($data as $v){
            $path = $v->path;
            //删除商品主图片
            @unlink('.'.$path);

        }
        $del = DB::table('pictures')->where('pid','=',$id)->delete();
        $res = DB::table('albums')->where('id','=',$id)->get()[0];
        $cover = $res->cover;
        @unlink('.'.$cover);
        $delete = DB::table('albums')->where('id','=',$id)->delete();
        if($delete){
            return redirect('/album/index');
        }else{
            return back();
        }
    }
}
